package com.naiyumi.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/** 
* <p>
* 日期时间处理类。
* </p>
*
* @author [作者] 
* @version [版本号, 20130320] 
*         修改记录 修改日期  修改者 修改内容 </br>
*         0        2013/03/20 yangcb 初稿
*/
public class DateUtil {
	public DateUtil() {
	}

	public static String getCurTime() {
		String formatDate = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		formatDate = sdf.format(new Date());
		return formatDate;
	}

	/**
	 * 计算指定月份的天数
	 * 
	 * @param month
	 *            月份
	 * @return 天数
	 */
	public static int getDayNumberOfMonth(String month) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date date = sdf.parse(month, pos);
		if (pos.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month + "无效");
		}
		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date);
		curCal.add(Calendar.MONTH, 1);
		int dd = curCal.get(Calendar.DATE);
		curCal.add(Calendar.DATE, -dd);
		dd = curCal.get(Calendar.DATE);
		return dd;
	}

	/**
	 * 比较给定的两个日期月份是否相同
	 * 
	 * @param month1
	 *            月份1
	 * @param month2
	 *            月份2
	 * @return 相同为true，否则为falst
	 */
	public static boolean isEqual(String month1, String month2)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month1, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month1 + "无效");
		}

		ParsePosition pos2 = new ParsePosition(0);
		Date date2 = sdf.parse(month2, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month2 + "无效");
		}

		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date1);
		int yy1 = curCal.get(Calendar.YEAR);
		int mm1 = curCal.get(Calendar.MONTH);
		curCal.setTime(date2);
		int yy2 = curCal.get(Calendar.YEAR);
		int mm2 = curCal.get(Calendar.MONTH);
		if (yy1 == yy2 && mm1 == mm2) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 比较month1是否小于month2
	 * 
	 * @param month1
	 *            月份1
	 * @param month2
	 *            月份2
	 * @return 小于为true，否则为falst
	 */
	public static boolean isLess(String month1, String month2) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month1, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month1 + "无效");
		}

		ParsePosition pos2 = new ParsePosition(0);
		Date date2 = sdf.parse(month2, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month2 + "无效");
		}

		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date1);
		int yy1 = curCal.get(Calendar.YEAR);
		int mm1 = curCal.get(Calendar.MONTH);
		curCal.setTime(date2);
		int yy2 = curCal.get(Calendar.YEAR);
		int mm2 = curCal.get(Calendar.MONTH);
		if (yy1 < yy2 || (yy1 == yy2 && mm1 < mm2)) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isLessEqual(String month1, String month2)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month1, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month1 + "无效");
		}

		ParsePosition pos2 = new ParsePosition(0);
		Date date2 = sdf.parse(month2, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month2 + "无效");
		}

		Calendar curCal = Calendar.getInstance();
		curCal.setTime(date1);
		int yy1 = curCal.get(Calendar.YEAR);
		int mm1 = curCal.get(Calendar.MONTH);
		curCal.setTime(date2);
		int yy2 = curCal.get(Calendar.YEAR);
		int mm2 = curCal.get(Calendar.MONTH);
		if (yy1 < yy2 || (yy1 == yy2 && mm1 <= mm2)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 计算从begin到end之间在month所指月份的天数
	 * 
	 * @param startMonth
	 * @param endMonth
	 * @param month
	 * @return
	 */
	public static int getDayNumberOfMonth(String begin, String end, String month)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date beginDate = sdf.parse(begin, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + begin + "无效");
		}
		Calendar beginCal = Calendar.getInstance();
		beginCal.setTime(beginDate);

		ParsePosition pos2 = new ParsePosition(0);
		Date endDate = sdf.parse(end, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + end + "无效");
		}
		if (endDate.before(getMinDate())) {
			endDate = new Date();
		}
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);

		ParsePosition pos3 = new ParsePosition(0);
		Date monthDate = sdf.parse(month, pos3);
		if (pos3.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month + "无效");
		}
		Calendar monthCal = Calendar.getInstance();
		monthCal.setTime(monthDate);

		Calendar monthFirstDate = Calendar.getInstance();
		monthFirstDate.setTime(monthDate);
		monthFirstDate.set(Calendar.DATE, 1);
		Calendar monthLastDate = Calendar.getInstance();
		monthLastDate.setTime(monthFirstDate.getTime());
		monthLastDate.add(Calendar.MONTH, 1);
		monthLastDate.add(Calendar.DATE, -1);

		if (beginCal.after(monthLastDate)) {
			return 0;
		}
		if (endCal.before(monthFirstDate)) {
			return 0;
		}
		if (beginCal.after(monthFirstDate)) {
			monthFirstDate = beginCal;
		}
		if (endCal.before(monthLastDate)) {
			monthLastDate = endCal;
		}

		int dd1 = monthFirstDate.get(Calendar.DATE);
		int dd2 = monthLastDate.get(Calendar.DATE);
		return dd2 - dd1 + 1;
	}

	/**
	 * 计算从startMonth到endMonth之间小于month所指日期的天数
	 * 
	 * @param startMonth
	 * @param endMonth
	 * @param month
	 * @return
	 */
	public static int getDayNumberToMonth(String startMonth, String endMonth,
			String month) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date startDate = sdf.parse(startMonth, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + startMonth + "无效");
		}
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startDate);

		ParsePosition pos2 = new ParsePosition(0);
		Date endDate = sdf.parse(endMonth, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + endMonth + "无效");
		}
		if (endDate.before(getMinDate())) {
			endDate = new Date();
		}
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);

		ParsePosition pos3 = new ParsePosition(0);
		Date monthDate = sdf.parse(month, pos3);
		if (pos3.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month + "无效");
		}
		Calendar monthCal = Calendar.getInstance();
		monthCal.setTime(monthDate);

		// 若month<begin ，返回0
		if (startCal.after(monthCal)) {
			return 0;
		}
		// 若月份大于end date ,则按end date 算
		if (endCal.before(monthCal)) {
			monthCal = endCal;
		}

		int num = 1;
		while (startCal.before(monthCal)) {
			startCal.add(Calendar.DATE, 1);
			num++;
		}
		return num;
	}

	/**
	 * 给定两个日期，返回日期的天数差
	 * 
	 * @param startMonth
	 * @param endMonth
	 * @return
	 * @throws java.lang.Exception
	 */
	public static int getDayNumberBetweenMonth(String startMonth,
			String endMonth) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date startDate = sdf.parse(startMonth, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + startMonth + "无效");
		}
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startDate);

		ParsePosition pos2 = new ParsePosition(0);
		Date endDate = sdf.parse(endMonth, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + endMonth + "无效");
		}
		if (endDate.before(getMinDate())) {
			endDate = new Date();
		}
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);

		if (startCal.after(endCal)) {
			return 0;
		}
		int num = 1;
		while (startCal.before(endCal)) {
			startCal.add(Calendar.DATE, 1);
			num++;
		}
		return num;
	}

	/**
	 * 给定的两个日期，返回介于这两个日期的月份（包括这两个日期月份）之间的月份的列表
	 * 
	 * @param month1
	 *            月份1
	 * @param month2
	 *            月份2
	 * @return 介于两个日期之间的所有月份
	 */
	public static List monthList(String month1, String month2) throws Exception {
		ArrayList list = new ArrayList();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month1, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month1 + "无效");
		}

		ParsePosition pos2 = new ParsePosition(0);
		Date date2 = sdf.parse(month2, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month2 + "无效");
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		cal1.set(Calendar.DATE, 1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		cal2.set(Calendar.DATE, 2);

		while (cal1.before(cal2)) {
			list.add(cal1.get(Calendar.YEAR) + "-"
					+ (cal1.get(Calendar.MONTH) + 1) + "-"
					+ cal1.get(Calendar.DATE));
			cal1.add(Calendar.MONTH, 1);
		}
		return list;
	}

	/**
	 * 
	 * 给定两个日期，返回其中较小的一个
	 * 
	 * @param month1
	 * @param month2
	 * @return
	 */
	public static String minMonth(String month1, String month2)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month1, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month1 + "无效");
		}

		ParsePosition pos2 = new ParsePosition(0);
		Date date2 = sdf.parse(month1, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month2 + "无效");
		}

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		if (cal1.after(cal2)) {
			return month2;
		} else {
			return month1;
		}
	}

	/**
	 * 
	 * 给定初始日期，及一个整数，返回初始日期＋整数的日期
	 * 
	 * @param month1
	 * @param dd
	 * @return
	 */
	public static String addDate(String date, int field, int dd)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(date, pos1);

		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + date + "无效");
		}

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		cal1.add(field, dd);
		return cal1.get(Calendar.YEAR) + "-" + (cal1.get(Calendar.MONTH) + 1)
				+ "-" + cal1.get(Calendar.DATE);

	}

	/**
	 * 获取指定月份的第一天的日期
	 * 
	 * @param month1
	 * @return
	 */
	public static String getFirstDayOfMonth(String month) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month, pos1);

		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month + "无效");
		}

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		return cal1.get(Calendar.YEAR) + "-" + cal1.get(Calendar.MONTH) + "-1";
	}

	private static Date getMinDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date dd = sdf.parse("1990-1-1", pos1);
		return dd;
	}

	public static String getCurrentFormatDate() {
		return getCurrentFormatDate("yyyy-MM-dd");
	}
	public static String getCurrentFormatDate(String format) {
	    String formatDate = "";
	    SimpleDateFormat sdf = new SimpleDateFormat(format);
	    formatDate = sdf.format(new Date());
	    return formatDate;
	}

	public static Date parseDateStr(String dateStr) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException ex) {
			return null;
		}
		return date;
	}

	public static Date parseDate(String dateStr) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyyMMddHHssmm");
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException ex) {
			return null;
		}
		return date;
	}

	public static java.sql.Date parseSQLDate(String dateStr) {
		Date d = parseDate(dateStr);
		if (d != null) {
			java.sql.Date sq = new java.sql.Date(d.getTime());
			return sq;
		}
		return null;
	}

	public static java.sql.Timestamp string2Time(String dateString) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyyMMddHHmmss");
		java.util.Date timeDate;
		try {
			timeDate = sdf.parse(dateString);
			java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate
					.getTime());
			return dateTime;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 格式化输出日期
	 * 
	 * @param date
	 *            Date
	 * @return String
	 */

	public static String toChar(Date date) {
		String formatDate = null;
		if (date != null) {
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyy-MM-dd");
			formatDate = sdf.format(date);
		} else {
			formatDate = "";
		}
		return formatDate;
	}
	
	
	public static String toCharStr(Date date) {
		String formatDate = null;
		if (date != null) {
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyyMMdd");
			formatDate = sdf.format(date);
		} else {
			formatDate = "";
		}
		return formatDate;
	}
	
	public static String toDateTime(Date date) {
		String formatDate = null;
		if (date != null) {
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyy-MM-dd hh:mm:ss");
			formatDate = sdf.format(date);
		} else {
			formatDate = "";
		}
		return formatDate;
	}
	


	/**
	 * 给定初始日期，及一个整数，返回初始日期＋整数的日期
	 * 
	 * @param date
	 *            Date
	 * @param field
	 *            String
	 * @param dd
	 *            int
	 * @return Date
	 */
	public static Date addDate(Date date, int field, int dd) throws Exception {
		return parseDateStr(addDate(toChar(date), field, dd));
	}

	public static List getBetweenDates(Date start, Date end) throws Exception {
		List list = new ArrayList();
		for (Date date = start; date.compareTo(end) <= 0; date = addDate(date,
				Calendar.DATE, 1)) {
			list.add(date);
		}
		return list;
	}

	// 获取月区间时间序列

	public static List getMonths(Date start, Date end) {
		List list = new ArrayList();
		Calendar c = Calendar.getInstance();
		Date endFirDay = DateUtil.parseDateStr(DateUtil.toChar(end).substring(
				0, 7)
				+ "-01");
		Date endLastDay = null;
		try {
			endLastDay = DateUtil.addDate(DateUtil.addDate(endFirDay,
					Calendar.MONTH, 1), Calendar.DATE, -1);
			for (Date i = start; i.compareTo(endLastDay) <= 0; i = DateUtil
					.addDate(i, Calendar.MONTH, 1)) {
				list.add(DateUtil.toChar(i).substring(0, 7));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return list;
	}

	public static List getYears(Date start, Date end) {
		List list = new ArrayList();
		Calendar c = Calendar.getInstance();
		Date endFirDay = DateUtil.parseDateStr(DateUtil.toChar(end).substring(
				0, 7)
				+ "-01");
		Date endLastDay = null;
		try {
			endLastDay = DateUtil.addDate(DateUtil.addDate(endFirDay,
					Calendar.MONTH, 1), Calendar.DATE, -1);
			for (Date i = start; i.compareTo(endLastDay) <= 0; i = DateUtil
					.addDate(i, Calendar.YEAR, 1)) {
				list.add(DateUtil.toChar(i).substring(0, 4));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return list;
	}

	public static List monthListDesc(String month1, String month2)
			throws Exception {
		ArrayList list = new ArrayList();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos1 = new ParsePosition(0);
		Date date1 = sdf.parse(month1, pos1);
		if (pos1.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month1 + "无效");
		}

		ParsePosition pos2 = new ParsePosition(0);
		Date date2 = sdf.parse(month2, pos2);
		if (pos2.getIndex() <= 0) {
			throw new Exception("指定的日期：" + month2 + "无效");
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		cal1.set(Calendar.DATE, 1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		cal2.set(Calendar.DATE, 2);

		while (cal1.before(cal2)) {
			list.add(cal2.get(Calendar.YEAR) + "-"
					+ (cal2.get(Calendar.MONTH) + 1) + "-"
					+ cal2.get(Calendar.DATE));
			cal2.add(Calendar.MONTH, -1);
		}
		return list;
	}

	private static Map short_months = null;
	static {
		short_months = new HashMap();
		short_months.put("Jan", "01");
		short_months.put("Feb", "02");
		short_months.put("Mar", "03");
		short_months.put("Apr", "04");
		short_months.put("May", "05");
		short_months.put("Jun", "06");
		short_months.put("Jul", "07");
		short_months.put("Aug", "08");
		short_months.put("Sep", "09");
		short_months.put("Oct", "10");
		short_months.put("Nov", "11");
		short_months.put("Dec", "12");
	}

	public static String parseYYYYMMDD(String cstDate) {
		// Thu Jan 17 04:52:07 CST 2008
		// 'Sun', 'Mon', 'Tue','Wed', 'Thu', 'Fri', 'Sat'
		// 'Jan', 'Feb', 'Mar', 'Apr','May', 'Jun', 'Jul', 'Aug', 'Sep',
		// 'Oct','Nov', 'Dec', 'Und'
		// modify by pp 2008/01/17 修改cstDate为空时，函数返回空
		// if (cstDate==null)
		if (cstDate == null || cstDate.equals("")) {
			// end by pp 2008/01/17
			return "";
		}
		String[] dff = cstDate.split(" ");

		String day = dff[2];
		String year = dff[5];
		String month_s = dff[1];

		String month = "";
		if (short_months.containsKey(month_s)) {
			month = short_months.get(month_s).toString();
		}

		String date = year + "-" + month + "-" + day;

		return date;
	}

	/**
	 * 比较两个日期的大小 如果date1>date2,返回大于 0 的值 如果date1<date2,返回小于 0 的值
	 * 如果date1=date2,返回等于 0 的值
	 * 
	 * @param date1
	 * @param date2
	 * @author 潘元鹏
	 * @date 2008-02-04
	 * @return
	 */
	public static int compareDate(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return -1;
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		return cal1.compareTo(cal2);
	}
	
	//得到当前时间的long值，并同步
	public static synchronized long getTimeNum() {
		return System.currentTimeMillis();
	}
	
	//是否闰年 
	public static boolean isLeapYear(int year) {    
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);    
	} 
	
	//获取指定账期的结束日期
	public static String getLastDayByCycle(String cycle) {
		int year = Integer.parseInt(cycle.substring(0,4));
		int month = Integer.parseInt(cycle.substring(4));
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return cycle + 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			return cycle + 30;
		}
		if (month == 2) {
			if (isLeapYear(year)) {
				return cycle + 29;
			} else {
				return cycle + 28;
			}
		}
		return "输入账期格式错误，正确格式：200901";
	} 
	
	
	/**
	 * @methodName: getContinuousDay
	 * @description 得到两日期相隔天数的数组
	 * @author: liuqf
	 * @date: 2010-5-18 下午02:56:49
	 * @param startDate
	 * @param endDate
	 * @return
	*/
	public static String[] getContinuousDay(Date startDate, Date endDate) {
		int days = daysBetween(startDate,endDate);
		String[] arr = new String[days + 1];
		Calendar calendar = Calendar.getInstance();   
		calendar.setTime(startDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		arr[0] = dateFormat.format(calendar.getTime());
		for (int i = 1; i < days + 1; i++) {
			calendar.add(Calendar.DATE,1);
			arr[i] = dateFormat.format(calendar.getTime());
		}
		return arr;
	}
	
	/**
	 * @methodName: daysBetween
	 * @description 得到两日期相差天数
	 * @author: liuqf
	 * @date: 2010-5-18 下午02:57:08
	 * @param early
	 * @param late
	 * @return
	*/
	public static int daysBetween(Date early, Date late) { 
		Calendar calst = Calendar.getInstance();   
		Calendar caled = Calendar.getInstance();   
        calst.setTime(early);   
        caled.setTime(late);   
        //设置时间为0时   
        calst.set(Calendar.HOUR_OF_DAY, 0);   
        calst.set(Calendar.MINUTE, 0);   
        calst.set(Calendar.SECOND, 0);   
        caled.set(Calendar.HOUR_OF_DAY, 0);   
        caled.set(Calendar.MINUTE, 0);   
        caled.set(Calendar.SECOND, 0);   
        //得到两个日期相差的天数   
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;   
        return days;   
   }   
}
